<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备名称" prop="equipmentName">
        <el-input
          v-model="queryParams.equipmentName"
          placeholder="请输入设备名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="位号" prop="equipmentCode">
        <el-input
          v-model="queryParams.equipmentCode"
          placeholder="请输入位号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['equipment:fighting:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['equipment:fighting:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['equipment:fighting:remove']"
        >删除</el-button>
      </el-col>
	  <el-col :span="1.5">
	    <el-button
	      type="info"
	      plain
	      icon="Upload"
	      @click="handleImport"
	      v-hasPermi="['equipment:fighting:export']"
	    >导入</el-button>
	  </el-col>
	  
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['equipment:fighting:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

	<el-card class="card-box">
    <el-table v-loading="loading" :data="fightingList" @selection-change="handleSelectionChange">
	  <el-table-column type="selection" width="55" align="center" fixed="left" />
	  <el-table-column label="序号" width="55" type="index" align="center" prop="id" fixed="left" />
      <el-table-column label="设备名称" align="center" prop="equipmentName" />
      <el-table-column label="位号" align="center" prop="equipmentCode" />
      <el-table-column label="设备型号" align="center" prop="equipmentModel" />
      <el-table-column label="安装位置" align="center" prop="installLocation" />
	  <el-table-column label="投用日期" align="center" prop="onlineDate" width="150" />
	  
      <el-table-column label="制造商" align="center" prop="makeFactory" />
      <el-table-column label="出厂编号" align="center" prop="makeNo" />
      <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
    <!--  <el-table-column label="创建人" align="center" prop="createdBy" />
	  <el-table-column label="创建时间" align="center" prop="createdDate" width="180" /> -->
      
	  <el-table-column label="操作" align="center" width="160" class-name="small-padding fixed-width" fixed="right">
	     <template #default="scope">
			<el-button
				size="mini"
				type="text"
				icon="el-icon-edit"
				@click="handleUpdate(scope.row)"
				v-hasPermi="['equipment:fighting:edit']"
			  >修改</el-button>
			  <el-button
				size="mini"
				type="text"
				icon="el-icon-delete"
				@click="handleDelete(scope.row)"
				v-hasPermi="['equipment:fighting:remove']"
			  >删除</el-button>
	     </template>
	  </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
	</el-card>

    <!-- 添加或修改消防设施对话框 -->
    <el-dialog :title="title" v-model="open" width="55%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px" style="margin-right: 90px">
		
		<el-row>
		   <el-col :span="12">
			   <el-form-item label="设备名称" prop="equipmentName">
			     <el-input v-model="form.equipmentName" placeholder="请输入设备名称" />
			   </el-form-item>
		   </el-col>
		   <el-col :span="12">
			   <el-form-item label="位号" prop="equipmentCode">
			     <el-input v-model="form.equipmentCode" placeholder="请输入位号" />
			   </el-form-item>
		   </el-col>
		</el-row>
		  
		<el-row>
		   <el-col :span="12">
			   <el-form-item label="设备型号" prop="equipmentModel">
			     <el-input v-model="form.equipmentModel" placeholder="请输入设备型号" />
			   </el-form-item>
		   </el-col>
		   <el-col :span="12">
			   <el-form-item label="安装位置" prop="installLocation">
			     <el-input v-model="form.installLocation" placeholder="请输入安装位置" />
			   </el-form-item>
		   </el-col>
		</el-row>
		
		<el-row>
		   <el-col :span="12">
			   <el-form-item label="投用日期" prop="onlineDate">
			     <el-date-picker clearable v-model="form.onlineDate" type="date" placeholder="请选择投用日期" :size="size" value-format="YYYY-MM-DD" format="YYYY-MM-DD" style="width: 100%;">
			     </el-date-picker>
			   </el-form-item>
		   </el-col>
		   <el-col :span="12">
			   <el-form-item label="制造商" prop="makeFactory">
			     <el-input v-model="form.makeFactory" placeholder="请输入制造商" />
			   </el-form-item>
		   </el-col>
		</el-row>
		
		<el-row>
		   <el-col :span="12">
			   <el-form-item label="出厂编号" prop="makeNo">
				 <el-input v-model="form.makeNo" placeholder="请输入出厂编号" />
			   </el-form-item>
		   </el-col>
		   <el-col :span="12">
			<el-form-item label="备注" prop="remark">
			   <el-input v-model="form.remark" placeholder="请输入备注" />
			</el-form-item>
		   </el-col>
		</el-row>
      </el-form>
	  
	  <template #footer>
		<div class="dialog-footer">
		  <el-button type="primary" @click="submitForm">确 定</el-button>
		  <el-button @click="cancel">取 消</el-button>
		</div>
	  </template>
    </el-dialog>
	
	
	<!-- 导入对话框 -->
	<el-dialog :title="upload.title" v-model="upload.open" width="400px" append-to-body>
	   <el-upload ref="uploadRef" :limit="1" accept=".xlsx, .xls" :headers="upload.headers"
		  :action="upload.url + '?updateSupport=' + upload.updateSupport" :disabled="upload.isUploading"
		  :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :auto-upload="false" drag>
		  <el-icon class="el-icon--upload"><upload-filled /></el-icon>
		  <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
		  <template #tip>
			 <div class="el-upload__tip text-center">
				<div class="el-upload__tip">
				   <el-checkbox v-model="upload.updateSupport" />是否更新已经存在的用户数据
				</div>
				<span>仅允许导入xls、xlsx格式文件。</span>
				<el-link type="primary" :underline="false" style="font-size: 12px; vertical-align: baseline" @click="importTemplate">下载模板</el-link>
			 </div>
		  </template>
	   </el-upload>
	   <template #footer>
		  <div class="dialog-footer">
			 <el-button type="primary" @click="submitFileForm">确 定</el-button>
		  </div>
	   </template>
	</el-dialog>
	
  </div>
</template>

<script>
import { listFighting, getFighting, delFighting, addFighting, updateFighting } from "@/api/aqsc/tzss/fighting";
import { getToken } from "@/utils/auth";

export default {
  name: "Fighting",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 消防设施表格数据
      fightingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        equipmentName: null,
        equipmentCode: null,
        equipmentModel: null,
        installLocation: null,
        onlineDate: null,
        makeFactory: null,
        makeNo: null,
        createdBy: null,
        createdDate: null,
        modifyBy: null,
        modifyDate: null,
      },
      // 表单参数
      form: {},
	  /*** 导入参数 */
	  upload: {
	     // 是否显示弹出层（用户导入）
	     open: false,
	     // 弹出层标题（用户导入）
	     title: "",
	     // 是否禁用上传
	     isUploading: false,
	     // 是否更新已经存在的用户数据
	     updateSupport: 0,
	     // 设置上传的请求头部
	     headers: { Authorization: "Bearer " + getToken() },
	     // 上传的地址
	     url: '/dev-api/equipment/fighting/importData',
	  },
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询消防设施列表 */
    getList() {
      this.loading = true;
      listFighting(this.queryParams).then(response => {
        this.fightingList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        equipmentName: null,
        equipmentCode: null,
        equipmentModel: null,
        installLocation: null,
        onlineDate: null,
        makeFactory: null,
        makeNo: null,
        remark: null,
        createdBy: null,
        createdDate: null,
        modifyBy: null,
        modifyDate: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加消防设施";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getFighting(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改消防设施";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateFighting(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFighting(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除消防设施编号为"' + ids + '"的数据项？').then(function() {
        return delFighting(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	
	
	handleImport() {
	   this.upload.title = "主要设备导入";
	   this.upload.open = true;
	},
	
	/** 下载模板操作 */
	importTemplate() {
		this.download('equipment/fighting/importTemplate', {
		}, `equipment_template_${new Date().getTime()}.xlsx`)
	},
		  
	/**文件上传中处理 */
	handleFileUploadProgress(event, file, fileList) {
	   this.upload.isUploading = true;
	},
	// /** 文件上传成功处理 */
	handleFileSuccess(response, file, fileList) {
	   this.upload.open = false;
	   this.upload.isUploading = false;
	   this.$refs["uploadRef"].handleRemove(file);
	   this.$alert(
	      "<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;word-wrap: break-word;'>" +
	      response.msg +
	      "</div>",
	      // "<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" +
	      // // response.msg  +
	      // response.msg.replace(/(.{1,100})/g, "$1<br>") +
	      // "</div>",
	      "导入结果",
	      { dangerouslyUseHTMLString: true }
	   );
	   this.getList();
	},
	
	/** 提交上传文件 */
	submitFileForm() {
	   this.$refs["uploadRef"].submit();
	},
	
	
    /** 导出按钮操作 */
    handleExport() {
	  if (this.ids) {
		  this.queryParams.ids = this.ids.join(",");
	  }
      this.download('equipment/fighting/export', {
        ...this.queryParams
      }, `fighting_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
